package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.${EntityName}QueryParam;
import com.macro.mall.dto.${EntityName}Vo;
import com.macro.mall.model.${EntityName};
import com.macro.mall.service.${EntityName}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ${classDescription}Controller
 * Created by ${user} on ${date}.
 */
@Controller
@Api(tags = "${EntityName}Controller", description = "${classDescription}")
@RequestMapping("/${entityName}")
public class ${EntityName}Controller {
    @Autowired
    private ${EntityName}Service ${entityName}Service;

    @ApiOperation("添加${addDesc}")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody ${EntityName} ${entityName}) {
        int count = ${entityName}Service.create(${entityName});
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改${addDesc}")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody ${EntityName} ${entityName}) {
        int count = ${entityName}Service.update(id, ${entityName});
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("批量删除${addDesc}")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = ${entityName}Service.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("获取所有${addDesc}")
    @RequestMapping(value = "/listAll", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<${EntityName}>> listAll() {
        List<${EntityName}> roleList = ${entityName}Service.list();
        return CommonResult.success(roleList);
    }

    @ApiOperation("分页获取${addDesc}列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<${EntityName}Vo>> listVo(@RequestBody ${EntityName}QueryParam ${entityName}QueryParam,
                                                  @RequestParam(value = "pageSize", defaultValue = "50") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<${EntityName}Vo> ${entityName}List = ${entityName}Service.list(${entityName}QueryParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(${entityName}List));
    }


}
